// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <pollux/common/file/file.h>
#include <pollux/external/hdfs/hdfs.h>

namespace kumo::pollux {

namespace filesystems::arrow::io::internal {
class LibHdfsShim;
}

/// Implementation of hdfs write file. Nothing written to the file should be
/// read back until it is closed.
class HdfsWriteFile : public WriteFile {
 public:
  /// The constructor.
  /// @param hdfsClient The configured hdfs filesystem handle.
  /// @param path The file path to write.
  /// @param bufferSize Size of buffer for write - pass 0 if you want
  /// to use the default configured values.
  /// @param replication Block replication - pass 0 if you want to use
  /// the default configured values.
  /// @param blockSize Size of block - pass 0 if you want to use the
  /// default configured values.
  HdfsWriteFile(
      filesystems::arrow::io::internal::LibHdfsShim* driver,
      hdfsFS hdfsClient,
      std::string_view path,
      int bufferSize = 0,
      short replication = 0,
      int blockSize = 0);

  ~HdfsWriteFile() override;

  /// Get the file size.
  uint64_t size() const override;

  /// Flush the data.
  void flush() override;

  /// Write the data by append mode.
  void append(std::string_view data) override;

  /// Close the file.
  void close() override;

 private:
  filesystems::arrow::io::internal::LibHdfsShim* driver_;
  /// The configured hdfs filesystem handle.
  hdfsFS hdfsClient_;
  /// The hdfs file handle for write.
  hdfsFile hdfsFile_;
  /// The hdfs file path.
  const std::string filePath_;
};
} // namespace kumo::pollux
